package com.hk.Mgr.web.controller.app.yingmi;

/**
 * Created by lipei on 2017/9/25.
 *
 * 用于统一控制盈米订单相关的信息处理
 *
 *
 */

import com.hk.app.utils.YingmiOpenAPI;
import com.hk.commons.StringHelper;
import com.hk.yingmi.YingmiOpenAPIConstant;
import com.hk.yingmi.YingmiProductCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

/**
 * Created by lipei on 2017/9/25.
 */
@Component
public class FundOrderUtil   {


    @Autowired
    private YingmiOpenAPI openAPI;


    @Autowired
    YingmiProductCache yingmiProductCache;

    private static Logger logger = LoggerFactory.getLogger(FundOrderUtil.class);

    //订单类型券商产品
    private static String ORDER_TYPE_BROKER;

    //订单类型基金产品
    private static String ORDER_TYPE_FUND;

    /**
     * 获取持有中的订单
     * @param brokerUserId
     * @param accountId
     * @param fundCode
     * @param shareId
     * @param fundType
     * @return
     */
    public List<Map> getOrderShares(String brokerUserId, String accountId, String fundCode, String shareId, String fundType){

        List<Map> resultList = new ArrayList<>();

        Map<String, String> parMap = new HashMap<>();

        parMap.put("brokerUserId", brokerUserId);
        parMap.put("accountId", accountId);

        if(StringHelper.isNotEmpty( fundCode)){
            parMap.remove("fundCode");
        }

        String orderListStr = openAPI.sslGet_ByMap(YingmiOpenAPIConstant.FUND_SHARES, parMap, null);

        ArrayList<Map> orderListMap = openAPI.getArrayList(orderListStr);


        //检查是否是券商类产品
        yingmiProductCache.isBrokerProduct(fundCode);




        if(StringHelper.isNotEmpty(shareId) && openAPI.isJsonArray(orderListStr)){
            Map order  =  orderListMap.stream().filter(map -> map.get("shareId").equals(shareId)).collect(Collectors.toList()).get(0);

            if(order != null){
                 resultList.add(order);
            }
        }else{
            resultList = orderListMap;
        }

        return  resultList;
    }
}
